package com.copm.ifm.servers.sys.controller;

import com.copm.ifm.base.api.pojo.PageDTO;
import com.copm.ifm.base.api.pojo.PageVO;
import com.copm.ifm.base.basic.ServiceResponse;
import com.copm.ifm.base.basic.pojo.DictContent;
import com.copm.ifm.base.service.base.BaseController;
import com.copm.ifm.servers.sys.client.SysDictClient;
import com.copm.ifm.servers.sys.dto.SysDictPageDTO;
import com.copm.ifm.servers.sys.entity.SysDict;
import com.copm.ifm.servers.sys.service.process.SysDictService;
import com.copm.ifm.servers.sys.vo.SysDictPageVO;
import com.copm.ifm.servers.sys.vo.SysDictVO;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 字典表相关接口类
 *
 * @author zzf
 * @date 2020/7/20 15:18
 */
@Api(tags = "字典表/sys-dict")
@RestController
@RequestMapping("/sys-dict")
public class SysDictController extends BaseController implements SysDictClient {

    @Autowired
    SysDictService service;

    @Override
    @ApiOperation("分页")
    @PostMapping("/page")
    public ServiceResponse<PageVO<SysDictPageVO>> page(@RequestBody PageDTO<SysDictPageDTO> param) {
        PageVO<SysDictPageVO> result = service.selectForPage(param);
        return success(result);
    }

    @Override
    @ApiOperation(value = "根据id查询数据", notes = "语言 [1简体中文 2繁体中文 3英文]")
    @GetMapping("/byId")
    @ApiImplicitParams({@ApiImplicitParam(value = "主键id", name = "id", required = true),
            @ApiImplicitParam(value = "语言", name = "language", required = true)})
    public ServiceResponse<SysDictVO> getById(@RequestParam Long id,
                                              @RequestParam int language) {
        return success(service.selectById(id, language));
    }

    @ApiOperation("删除-根据id")
    @DeleteMapping("/by/id")
    public ServiceResponse<?> deleteById(@ApiParam(value = "主键id", required = true) @RequestParam Long id) {
        //修改delete字段为1的删除方式
        service.updateDeleted(id);
        return success();
    }

    @ApiOperation(value = "根据code查询数据", notes = "语言 [1简体中文 2繁体中文 3英文]")
    @GetMapping("/byCode")
    @ApiImplicitParams({@ApiImplicitParam(value = "编码", name = "code", required = true),
            @ApiImplicitParam(value = "语言", name = "language", required = true)})
    public ServiceResponse<SysDictVO> getByCode(@RequestParam String code,
                                                @RequestParam int language) {
        return success(service.selectByCode(code, language));
    }

    @ApiOperation(value = "根据name查询数据", notes = "语言 [1简体中文 2繁体中文 3英文]")
    @GetMapping("/byName")
    @ApiImplicitParams({@ApiImplicitParam(value = "名称", name = "name", required = true),
            @ApiImplicitParam(value = "语言", name = "language", required = true)})
    public ServiceResponse<SysDictVO> getByName(@RequestParam String name,
                                                @RequestParam int language) {
        return success(service.selectByName(name, language));
    }

    @Override
    @ApiOperation("新增保存")
    @PostMapping
    public ServiceResponse<SysDict> save(@RequestBody SysDict entity) {
        service.save(entity);
        return success(entity);
    }

    @Override
    @ApiOperation("更新保存")
    @PutMapping("/update")
    public ServiceResponse<SysDict> update(@RequestBody SysDict entity) {
        service.updateEntityById(entity);
        return success(entity);
    }

    @ApiOperation("字典表根据code添加content值")
    @PutMapping
    @ApiImplicitParams({@ApiImplicitParam(value = "content对象", name = "entity", required = true),
            @ApiImplicitParam(value = "编码", name = "code", required = true)})
    public ServiceResponse<SysDict> update(@RequestBody DictContent entity,
                                           @RequestParam String code) {
        SysDict sysDictNew = service.updateSys(entity, code);
        service.updateById(sysDictNew);
        return success(sysDictNew);
    }

    @ApiOperation("根据code删除字典表内部content值")
    @DeleteMapping
    @ApiImplicitParams({@ApiImplicitParam(value = "content对象的key值", name = "key", required = true),
            @ApiImplicitParam(value = "编码", name = "code", required = true)})
    public ServiceResponse<?> deleteByKey(@RequestParam Integer key,
                                          @RequestParam String code) {
        service.deletedByKey(key, code);
        return success();
    }

}
